<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i>故事与回忆列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" round @click="showAdd"><i class="el-icon-plus"> </i>&nbsp;&nbsp;发布故事与回忆</el-button>
            <div style="height:40px"></div>
                <el-form :inline="true"  class="demo-form-inline">
                    <el-form-item label="标题:">
                        <el-input v-model="title" placeholder="请输入问题名称" clearable style="width:300px;"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSelect"> &nbsp;&nbsp;查询&nbsp;&nbsp;  </el-button>
                        <!-- <el-button type="primary" @click="onResetting"> &nbsp;&nbsp;重置&nbsp;&nbsp;  </el-button> -->
                    </el-form-item>
                </el-form>
                <el-table :data="tableData"  style="width: 100%"   ref="multipleTable">
                    <el-table-column prop="id" label="ID" width="120" align="center">
                    </el-table-column>
                    <el-table-column label="封面图" width="120" align="center">
                        <template slot-scope="scope" >
                            <div><img :src="scope.row.head_img" style="width:120px;height:60px"></div>
                    </template>
                    </el-table-column>
                    <el-table-column  prop="title" label="标题" width="600" align="center">
                    </el-table-column>
                    <el-table-column  prop="author" label="作者" width="120" align="center">
                    </el-table-column>
                    <el-table-column  prop="source" label="来源" width="120" align="center">
                    </el-table-column>
                    <el-table-column  prop="authorize" label="类型" width="150" align="center">
                    </el-table-column>
                     <el-table-column prop="create_date" label="创建时间" width="130" align="center">
                    </el-table-column>
                    <el-table-column label="操作" align="center" >
                        <template slot-scope="scope" >
                            <el-button class="look" size="small" type="text" @click="handleLook(scope.$index, scope.row)">查看</el-button>
                            <el-button class="edit" size="small" type="text" @click="showEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button class="del" size="small" type="text"  @click="handleDelete(scope.$index, scope.row)" >删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination" v-show="totalPages>1">
                    <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next" :page-size="pageSize" :total="totalElements" background>
                    </el-pagination>
                </div>
                
        </div>
        <!-- 删除列表提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
        <!-- / 删除列表提示框 -->
    </div>
</template>
<script>
import { getStoryList,postDeleteStory,postAddStory,postEditStory } from '@/api/index/storyMemory'
export default {
    name:"app",
    data() {
        return {
            pageIndex:1,//当前页码
            pageSize:10,//每页10条
            totalElements:1,//默认的总条数
            totalPages:1,//默认的总页数
            tableData:[],//表格数据
            delIndex:0, //当前选中的下标
            delId:0,  //当前要删除的Id
            delVisible:false, //默认dialog 不弹出
            title:'', //文章标题
            isAddAccout:false,//是否弹出新增编辑界面
            id:'',//当前问题id
            issue:'',//编辑或新增的问题
            result:'',//编辑或新增的答案
            dialogState:false,//弹出框的状态 false代表新增 true是修改
        }
    },
    //实例创建
    created(){
        //获得表格数据
        this.getDate();
    },
    //计算属性
    computed:{

    },
    //方法
    methods: {
        //获取数据
        getDate(){
            getStoryList(this.title,this.pageIndex,this.pageSize).then((response)=> {
                console.log(response);
                if(response.code==200){
                    var data = response.data;//当前返回的数据
                    this.tableData = data.list;//列表数据
                    this.totalElements = data.total; //总条数
                    this.totalPages = Math.ceil(data.total/this.pageSize);//总页数
                }else{
                    this.$notify.error({
                        title: '异常',
                        offset: 100,
                        message: response.msg
                    });
                }
            })
        },
        //查看
        handleLook(index,row){
            //跳转到详情页
              if(row.id && row.id>0){//编辑状态
                this.$router.push({ path: '/index/storyMemory/details',query:{id:row.id}});
            }else{
                this.$notify.info({title: '提示',message: '获取详情ID失败，请重试！'});
            }              
        },
        //新增
        showAdd(){
           this.$router.push({path: '/index/storyMemory/add',query:{}});
        },
        //编辑
        showEdit(index,row){
            if(row.id && row.id>0){//编辑状态
                this.$router.push({path: '/index/storyMemory/edit',query:{id:row.id}});
            }else{
                this.$notify.info({title: '提示',message: '获取详情ID失败，请重试！'});
            }    
        },
    
        //查询
        onSelect(){
            this.pageIndex =1;
            this.getDate();
        },
        //分页
        handleCurrentChange(val){
            this.pageIndex =val;
            this.getDate();
        },
        //删除
        handleDelete(index,row){
            // console.log(index,row.id);
            if(row.id && row.id>0){
                this.delIndex =index;//要删除的下标
                this.delId = row.id; //要删除的ID
                this.delVisible = true; //显示兑换弹出框
                 //console.log(this.delVisible);
            }else{
                // console.log("没触发");
            }
        },
        // 确定删除
        deleteRow(){
            //console.log(this.delId)
            if(this.delId>0){
                // console.log(this.delId)
                postDeleteStory(this.delId).then(response=>{
                    // console.log(response);
                    if(response.code==200){
                        this.$message.success('删除成功');
                        this.delVisible = false;
                        this.getDate();
                    }else{
                        this.$notify.error({
                            title: '删除失败',
                            duration:2000,
                            message: '失败原因：'+response.data.msg
                        });
                    }
                    
                })
            }
            
        }
        
    }
}
</script>
<style lang="stylus" scoped>
    .edit{
        color #ff9800;
    }
    .del{
        color #e51c23;
    }
    .download{
        color #67C23A;
    }
    .sola-upload{
        .sola-left{
            width :100%;
            height :auto;
            .sola-flex{
                float:left
                width: 300px;
                height: 300px;
                margin-bottom 20px;
                margin-right:15px;
                position:relative;
                .delete{
                    position:absolute;
                    top:2px;
                    right:13px;
                    color: #f56c6c;
                    display:inline-block;
                    padding:5px 5px;
                    .el-alert__icon{font-size:28px;cursor:pointer}
                }
                img {
                    width:100%;
                    height:100%;
                }
            }
        }
        .sola-file-box{
            position:relative;
            .btn-file{
                position:absolute;
                top:0;
                left:0;
                z-index 1;
                opacity:0;
                width :120px;
                height :40px;
                outline: 0;
                background-color :none;
                cursor:pointer;
            }
        }
    }
</style>


